package cc.rengu.igas.bomp.core.model;

import cc.rengu.igas.acct.facade.base.Header;
import cc.rengu.igas.acct.facade.request.VirtAcctEstablishRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctEstablishResponse;
import cc.rengu.igas.acct.facade.result.Result;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

public class VirtAcctIntegration {

    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    public boolean virtAcctEstablish(MchntBaseInfo mchntBaseInfo, TxnInfo txnInfo) {
        boolean result = true;
        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            VirtAcctEstablishRequest virtAcctEstablishRequest = new VirtAcctEstablishRequest();

            Header acctHeader = new Header();
            acctHeader.setInstId(mchntBaseInfo.getInstId());
            acctHeader.setTxnNum("M00AT001");
            acctHeader.setTransDate(DateUtil.getCurrentDate());
            acctHeader.setTransTime(DateUtil.getCurrentTime());
            acctHeader.setTraceNo(sysSeqNum + "1");
            acctHeader.setSrcSysId(AppParamConstant.SYS_ID);
            acctHeader.setChanlId(ChannelEnum.BOMP.getChannelType());
            virtAcctEstablishRequest.setCustomerName(mchntBaseInfo.getMchntContactName());
            virtAcctEstablishRequest.setCertType(mchntBaseInfo.getMchntContactCertType());
            virtAcctEstablishRequest.setCertNo(mchntBaseInfo.getMchntContactCertNo());
            virtAcctEstablishRequest.setMobilePhone(mchntBaseInfo.getMchntContactPhone());
            //00-虚拟账户
            virtAcctEstablishRequest.setAcctType("00");
            //00-商户
            virtAcctEstablishRequest.setUserType("00");
            virtAcctEstablishRequest.setUserId(mchntBaseInfo.getMchntNo());
            virtAcctEstablishRequest.setHeader(acctHeader);
            DubboService dubboService = new DubboServiceImpl();
            Result<VirtAcctEstablishResponse> acctDubboResult = (Result<VirtAcctEstablishResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_VIRT_ACCT_SERVICE, "virtAcctEstablish", virtAcctEstablishRequest);
            if (null == acctDubboResult) {
                rglog.error("生成虚拟账户失败！商户入驻失败！重新进行审核操作！id:{},mchntNo:{},instId:{}", mchntBaseInfo.getId(), mchntBaseInfo.getMchntNo(), mchntBaseInfo.getInstId());
                throw new BizException(RespCodeEnum.CREATE_VIRT_ACCT_INFO_ERROR.getRespCode(), RespCodeEnum.CREATE_VIRT_ACCT_INFO_ERROR.getRespDesc());
            } else if (!acctDubboResult.isSuccess() || StringUtil.isEmptyOrNull(acctDubboResult.getResult().getCustomerId())) {
                //生成虚拟账户失败
                rglog.error("生成虚拟账户失败！商户入驻失败！重新进行审核操作！id:{},mchntNo:{},instId:{}，错误码：{},错误描述：{},错误消息：{}",
                        mchntBaseInfo.getId(), mchntBaseInfo.getMchntNo(), mchntBaseInfo.getInstId(), acctDubboResult.getResult().getRespCode(), acctDubboResult.getResult().getRespDesc(), acctDubboResult.getResult().getRespDesc());
                throw new BizException(acctDubboResult.getResult().getRespCode(), acctDubboResult.getResult().getRespDesc());
            }
        } catch (Exception e) {
            result = false;
            rglog.error("生成虚拟账户失败，instId:{},mchntNo:{}, 异常原因:{}",
                    mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), e.getMessage());
        }
        return result;
    }
}
